package com.jsgt.greekfire.ui.home.menu.powerise

import android.app.Activity
import android.text.Html
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.PrivilegeWalletBean
import com.jsgt.greekfire.common.util.PayPassWordUtil
import com.jsgt.greekfire.common.widget.maxDecimalPlace
import com.jsgt.greekfire.viewmodel.PrivilegeViewModel
import kotlinx.android.synthetic.main.activity_privilege_transfer.*
import kotlinx.android.synthetic.main.view_title.*
import java.math.BigDecimal

class PrivilegeTransferActivity : BaseActivity(), View.OnClickListener {
    private var privilegeType = 0  //0-云创智  1-云创联
    private var walletBean: PrivilegeWalletBean? = null

    companion object {
        const val INTENT_DATA = "data"
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_privilege_transfer
    }

    private val privilegeViewModel: PrivilegeViewModel by lazy {
        ViewModelProvider(this).get(PrivilegeViewModel::class.java)
    }

    override fun main() {
        privilegeType = intent.getIntExtra(PrivilegeActivity.TYPE_STR, 0)
        walletBean = intent.getParcelableExtra(INTENT_DATA)
        if (walletBean == null) {
            finish()
        }
        if (walletBean!!.assetName == null) {
            finish()
        }
        title_tv.text = getString(R.string.privilege_wallet_trans)
        right_tv.visibility = View.VISIBLE
        right_tv.text = if (privilegeType == 0) {
            getString(R.string.powerise_income)
        } else {
            getString(R.string.cl_income)
        }
        right_tv.setOnClickListener(this)

        txvWallet.text = if (privilegeType == 0) {
            getString(R.string.powerise_wallet)
        } else {
            getString(R.string.cl_wallet)
        }
        setCoinTypeName(walletBean!!.assetName)

        txvMax.text =
            Html.fromHtml("${getString(R.string.privilege_wallet_trans_max)} <font color='#2F3140'>${walletBean!!.amount}${walletBean!!.assetName}</font>")
        txvAll.setOnClickListener(this)
        txvOk.setOnClickListener(this)
        edtAmount.maxDecimalPlace(8)

        privilegeViewModel.transferListData.observe(this, Observer {
            showProgress(false)
            showToast(getString(R.string.privilege_wallet_trans_success))
            setResult(Activity.RESULT_OK)
            finish()
        })
        privilegeViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            showToast(getString(R.string.privilege_wallet_trans_failed))
        })
    }

    private fun setCoinTypeName(coin: String?) {
        txvCoin.text = coin
        txvCoin1.text = coin
        txvCoin2.text = coin
        txvCoin3.text = coin
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.right_tv -> {
                showToast(getString(R.string.not_yet_open))
            }
            R.id.txvAll -> {
                edtAmount.setText(walletBean!!.amount)
                edtAmount.setSelection(edtAmount.text.length)
            }
            R.id.txvOk -> {
                if (!TextUtils.isEmpty(edtAmount.text.toString())) {
                    PayPassWordUtil.show(
                        this,
                        walletBean!!.assetName!!,
                        edtAmount.text.toString()
                    ) {
                        showProgress(true)
                        privilegeViewModel.privilegeWalletTransfer(
                            privilegeType,
                            walletBean!!.guid,
                            BigDecimal(edtAmount.text.toString()).toDouble(),
                            it
                        )
                    }
                }else{
                    showToast(getString(R.string.transfer_hint_num))
                }
            }
        }
    }
}